package com.doyens.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.doyens.reggie.commons.BaseContext;
import com.doyens.reggie.commons.R;
import com.doyens.reggie.domain.Employee;
import com.doyens.reggie.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 过滤器的使用步骤
 *  1.定义类实现javax.servlet.Filter接口
 *  2.重写doFilter()
 *  3.在类上添加@WebFilter注解 通过urlPatterns指定拦截路径
 *  4.在引导类上添加@ServletComponentScan注解
 */
@WebFilter(filterName = "CheckLoginFilter",urlPatterns = {"/*"})
@Slf4j
public class CheckLoginFilter implements Filter {

    //路径匹配器，支持通配符
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        // 获取请求路径
        String uri = request.getRequestURI();

        // 定义要放行的资源
        //定义不需要处理的请求路径
        String[] urls = new String[]{
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/common/**",
                "/user/**",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs"
        };

        boolean b = matchUri(urls,uri);
        if(b){// 说明访问的资源不拦截
            //放行
            filterChain.doFilter(servletRequest,servletResponse); //employee/page
            return;
        }
        // 说明访问的资源不放行  判断是否登录  如果登录了 再放行
        HttpSession session = request.getSession();
        Employee emp = (Employee) session.getAttribute("emp");
        if(emp != null){ //说明已经登录了
            BaseContext.setCurrentId(emp.getId());
            //放行
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }

        User user = (User) session.getAttribute("user");
        if(user != null){ //说明已经登录了
            BaseContext.setCurrentId(user.getId());
            //放行
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }


        // 说明没有登录 响应数据给浏览器  data  code  msg
        R<Object> r = R.error("NOTLOGIN"); //data:null code=0 msg=NOTLOGIN
        response.getWriter().write(JSON.toJSONString(r));
    }

    private boolean matchUri(String[] urls, String uri) {
        for (String url : urls) {
            boolean b = PATH_MATCHER.match(url, uri);
            if(b){
                return true;
            }
        }
        return false;
    }
}
